/**
 * 物品js
 */
$(function(){
	$("#goods").datagrid({
	    url:path+'goods/getallgoods.html',  
	    //queryParams:{},  
	    loadMsg:'数据加载中请稍后……',  
	    pagination: true, 
	    height:480,
	    remoteSort:false,
	    fit:true,
	    pageList: [15,20,30,40,50],
	    pageSize: 15,
	    rownumbers: true,  
	    fitColumns:true,
	    columns:[[ 
	        {field:'id',checkbox:true },
			{field:'goodsname',title: '物品名称',align: 'center',width:65,sortable:true},  
			{field:'specifications',title: '规格型号',align: 'center',width:65,sortable:true},
			{field:'billno',title: '物品编码',align: 'center',width:65,sortable:true},
			{field:'units',title: '单位',align: 'center',width:65,sortable:true},
			{field:'billdate',title: '创建时间',align: 'center',width:65,sortable:true,
				formatter:function(value,rowData,rowIndex){
	        		var s=jsonDateFormat(""+value.time+"");
	                return s; 
	 	           }  
				},  
			{field:'isused',title: '是否启用',align: 'center',width:65,sortable:true,
			   formatter:function(value,rowData,rowIndex){
				    var s="";
				    if(value==1){
				    	s="启用";
				    }
                    if(value==0){
				    	s="停用";
				    }
			        return s; 
			   } 	
			}
	    ]],	    
	    singleSelect : false,
	    selectOnCheck: true,
	    checkOnSelect: true,
	    onDblClickRow:function(rowIndex, rowData){
	    		
	    },
	    onLoadSuccess:function(date){
	    	if(role==1){ //如果角色是guest 不能编辑数据
	    		$(" .datagrid-toolbar").hide();
	    	}
	    	//$(".datagrid-header-check").attr("disabled","disabled");
	    },
		toolbar: [{
			iconCls: 'icon-add',
			text:'新增',
			handler: function(){
			   addGoods();
			}
		},'-',{
			iconCls: 'icon-edit',
			text:'编辑',
			handler: function(){
				var rowData = $('#goods').datagrid('getSelected');
				var selectrows=$("#goods").datagrid('getSelections');
				//alert(selectrows.length)
				if(selectrows.length==0){
					alert("请选择一条数据进行编辑！");
					$("#goods").datagrid("uncheckAll");
					return false;
				}else if(selectrows.length==1){
					if(role!=1){ //如果角色是guest 不能编辑数据
			    		Editor(rowData);
			    	}
				}else{
					alert("对不起！当前操作只能选择一条数据进行编辑！");
					$("#goods").datagrid("uncheckAll");
					return false;
				}
			}
		},'-',{
			iconCls: 'icon-remove',
			text:'删除',
			handler: function(){ 
				deldate();
			}
		}]

	}); 
	
	    var p = $('#goods').datagrid('getPager'); 
	    $(p).pagination({ 
	        pageSize: 15,//每页显示的记录条数，默认为10 
	        pageList: [15,20,30,40,50],//可以设置每页记录条数的列表 
	        beforePageText: '第',//页数文本框前显示的汉字 
	        afterPageText: '页    共 {pages} 页', 
	        displayMsg: '当前显示 {from} - {to} 条记录   共 {total} 条记录'
	    });     
		});


function jsonDateFormat(jsonDate) {
	   //json日期格式转换为正常格式
	    try {
	        var date = new Date(parseInt(jsonDate.replace("/Date(", "").replace(")/", ""), 10));
	        var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
	        var day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
	        var hours = date.getHours();
	        var minutes = date.getMinutes();
	        var seconds = date.getSeconds();
	        var milliseconds = date.getMilliseconds();
	        return date.getFullYear() + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds + "." + milliseconds;
	    } catch (ex) {
	        return "";
	    }
	}
/**
 * 新增物品
 */
function addGoods(){
	$('#goodsadd').window('open');
	$('#addgoodsform')[0].reset();
	$('#goodsadd').panel('setTitle','新增物品');
	$("#tag").attr("value","0");	
}

function add(obj){
	var tag=$("#tag").val();
	$.messager.progress();	// 显示进度条
	$('#addgoodsform').form('submit', {
		url: path+'goods/addgoods.html?tag='+tag, 
		onSubmit: function(){
			var isValid = $(this).form('validate');
			if (!isValid){
				$.messager.progress('close');	// 如果表单是无效的则隐藏进度条
			}
			return isValid;	// 返回false终止表单提交
		},
		success: function(data){
			$.messager.progress('close');	// 如果提交成功则隐藏进度条
			$('#goods').datagrid('reload');
			$('#goodsadd').window('close');
		}
	});	
}
/**
 * 删除数据
 */
function deldate(){
	var ids = $("#goods").datagrid('getSelections');
	var names = [];
	$.each(ids, function(index, item){
	    names.push(item.id);
	});
	if(names==''){
		alert("请选择数据后再点删除！");
		return false;
	}
	$.messager.confirm('确认','您确认想要删除选择记录吗？',function(r){    
	    if (r){    
	    	$.ajax({  
	            type:'post',      
	            url:path+'goods/updategoods.html',  
	            data:"goodsname="+names, 
	            cache:false,    
	            success:function(data){
	            	if(data=='ok'){
	            		alert("删除成功！");
	            	}
	            	$('#goods').datagrid('reload');
	            },
	    	    error:function(){   
	    	    	 
	    	    }
	        });    
	    }    
	});
}
/**
 * 编辑
 */
function Editor(obj){
	//  alert(obj.isused);
	$('#goodsadd').panel('setTitle','编辑物品');
	$('#goodsadd').window('open');
	$("#tag").val("1");
	$("#goodsname").val(obj.goodsname);
	$("#specifications").val(obj.specifications);
	//$('#specifications').combobox('setValue',obj.specifications);
	$("#id").val(obj.id);
	$('#units').combobox('setValue',obj.units);
	$('#isused').val(obj.isused);
}
/***
 * 选择字段查询数据
 * @param value
 * @param name
 */
function searcherGoods(value,name){
	var url=path+'goods/getallgoods.html?field='+name+"&searchertext="+value;
	url=encodeURI(url);
	url=encodeURI(url);
	$("#goods").datagrid("load",url);
}
